package matlap.client.view.distributor;

import matlap.client.services.user.UserService;
import matlap.client.services.user.UserServiceAsync;
import matlap.shared.User;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Button;

public class AccountComposite extends Composite {

	private VerticalPanel vp;
	UserServiceAsync us = GWT.create(UserService.class);
	private Label lbUser;
	private Label lbWho;
	private Label lbFirstName;
	private TextBox tbFirstName;
	private Label lbLastName;
	private TextBox tbLastName;
	private Label lbEmail;
	private TextBox tbEmail;
	private Label lbPhone;
	private TextBox tbPhone;
	private Label lbAdress;
	private TextBox tbAdress;

	private TextBox tbPassword;
	private Label lboldPassword;
	private TextBox tboldPassword;
	private Button btnSavePassword;

	private Button btnSave;

	public AccountComposite(final String who) {

		initPanel();
		us.getUser(who, new AsyncCallback<User>() {

			public void onFailure(Throwable caught) {

			}

			public void onSuccess(User result) {
				lbWho.setText(who);
				tbFirstName.setText(result.getFirstName());
				tbLastName.setText(result.getLastName());
				tbEmail.setText(result.getEmail());
				tbAdress.setText(result.getAddress());
				tbPhone.setText(result.getPhone());
			}

		});

		btnSave.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				User user = new User();
				user.setUserName(lbWho.getText());
				user.setFirstName(tbFirstName.getText());
				user.setLastName(tbLastName.getText());
				user.setEmail(tbEmail.getText());
				user.setAddress(tbAdress.getText());
				user.setPhone(tbPhone.getText());
				// String pass=
				// user.setPassword(pass);
				us.updateUser(user, new AsyncCallback<Void>() {

					public void onFailure(Throwable caught) {
					}

					public void onSuccess(Void result) {

					}

				});
				Window.alert("\u00DAj adatok elmentve");

			}

		});
		/*
		 * btnSavePassword.addClickHandler(new ClickHandler(){
		 * 
		 * public void onClick(ClickEvent event) { User user=new User();
		 * user.setUserName(lbWho.getText());
		 * user.setFirstName(tbLastName.getText());
		 * user.setLastName(tbFirstName.getText());
		 * user.setEmail(tbEmail.getText()); user.setAdress(tbAdress.getText());
		 * user.setPhone(tbPhone.getText());
		 * 
		 * us.updatePassword(user, new AsyncCallback(){
		 * 
		 * public void onFailure(Throwable caught) { }
		 * 
		 * public void onSuccess(Object result) {
		 * 
		 * }
		 * 
		 * }); Window.alert("\u00DAj adatok elmentve");
		 * 
		 * 
		 * }
		 * 
		 * });
		 * 
		 * /* btnSavePassword.addClickHandler(new ClickHandler(){
		 * 
		 * public void onClick(ClickEvent event) { User user=new User();
		 * user.setUserName(lbWho.getText());
		 * user.setFirstName(tbLastName.getText());
		 * user.setLastName(tbFirstName.getText());
		 * user.setEmail(tbEmail.getText()); user.setAdress(tbAdress.getText());
		 * user.setPhone(tbPhone.getText()); String
		 * pass=tboldPassword.getText(); us.updatePassword(user, pass, new
		 * AsyncCallback(){
		 * 
		 * public void onFailure(Throwable caught) { }
		 * 
		 * public void onSuccess(Object result) {
		 * 
		 * }
		 * 
		 * }); Window.alert("\u00DAj jelsz\u00F3 elmentve");
		 * 
		 * 
		 * }
		 * 
		 * });
		 */
	}

	public void initPanel() {
		vp = new VerticalPanel();
		initWidget(vp);
		vp.setSize("313px", "319px");

		Label lbAccount = new Label("Adataim");
		lbAccount.setStyleName("BigBold");
		vp.add(lbAccount);
		vp.setCellHorizontalAlignment(lbAccount,
				HasHorizontalAlignment.ALIGN_CENTER);

		FlexTable accountFlex = new FlexTable();
		vp.add(accountFlex);

		lbUser = new Label("Felhaszn\u00E1l\u00F3");
		accountFlex.setWidget(0, 0, lbUser);

		lbWho = new Label("");
		lbWho.setStyleName("BigBold");
		accountFlex.setWidget(0, 1, lbWho);

		lbLastName = new Label("Vezet\u00E9kn\u00E9v");
		accountFlex.setWidget(1, 0, lbLastName);

		tbLastName = new TextBox();
		accountFlex.setWidget(1, 1, tbLastName);

		lbFirstName = new Label("Keresztn\u00E9v");
		accountFlex.setWidget(2, 0, lbFirstName);

		tbFirstName = new TextBox();
		accountFlex.setWidget(2, 1, tbFirstName);

		lbEmail = new Label("E-mail");
		accountFlex.setWidget(3, 0, lbEmail);

		tbEmail = new TextBox();
		accountFlex.setWidget(3, 1, tbEmail);

		lbPhone = new Label("Telefon");
		accountFlex.setWidget(4, 0, lbPhone);

		tbPhone = new TextBox();
		accountFlex.setWidget(4, 1, tbPhone);

		lbAdress = new Label("C\u00EDm");
		accountFlex.setWidget(5, 0, lbAdress);

		tbAdress = new TextBox();
		accountFlex.setWidget(5, 1, tbAdress);

		btnSave = new Button("Ment\u00E9s");
		accountFlex.setWidget(6, 1, btnSave);

		Label lbPassword = new Label("Jelsz\u00F3m\u00F3dos\u00EDt\u00E1s");
		lbPassword.setStyleName("BigBold");
		vp.add(lbPassword);
		vp.setCellHorizontalAlignment(lbPassword,
				HasHorizontalAlignment.ALIGN_CENTER);

		FlexTable PasswordFlex = new FlexTable();
		vp.add(PasswordFlex);

		lboldPassword = new Label("R\u00E9gi jelsz\u00F3");
		PasswordFlex.setWidget(0, 0, lboldPassword);

		tboldPassword = new TextBox();
		PasswordFlex.setWidget(0, 1, tboldPassword);

		lbPassword = new Label("\u00DAj jelsz\u00F3");
		PasswordFlex.setWidget(1, 0, lbPassword);

		tbPassword = new TextBox();
		PasswordFlex.setWidget(1, 1, tbPassword);

		btnSavePassword = new Button("Jelsz\u00F3 ment\u00E9se");
		PasswordFlex.setWidget(2, 1, btnSavePassword);

	}

}
